<!-- 买入股票  -->
<template>
    <div class="buy_stock">
      <van-nav-bar
        title="买入股份"
        left-arrow
        @click-left="$router.back()"
        left-text="返回"
      />
      <com-buy-stock :needStockObj="needStockObj" @getBuyParam="getBuyParam"></com-buy-stock>
    </div>
</template>

<script>
  import {Toast} from 'vant';
  import comBuyStock from '../../components/comBuyStock';

  export default {
    name: 'buyStock',
    data(){
      return {
        needStockObj: ''
      }
    },
    created() {
      const obj = this.$route.params.obj || localStorage.getItem('needStockObj')
      // 若有obj, 则保存
      if (obj) {
        if (this.$route.params.obj) {
          localStorage.setItem('needStockObj', obj)
        }
        this.needStockObj = JSON.parse(localStorage.getItem('needStockObj'))
      } else {
        this.$router.push('/groupStock/stockMarket')
        localStorage.removeItem('needStockObj')
      }
    },

    computed: {
      computedStockPrice(){
        let price
        if (this.needStockObj){
          price = this.needStockObj
        } else {
          price = ''
        }
        return price
      }
    },

    methods: {
      getBuyParam(data){
        this.$api.stock.reqBuyStock(data).then(res =>{
          if(res.code===200){
            Toast(res.data)
            this.$router.push('/groupStock/stockMarket')
          }
        })
      }
    },

    components: {
      ComBuyStock: comBuyStock,
    }
  }
</script>

<style scoped>

</style>
